<?php
/**
 * Creater: 李青林
 * Date Time : 2018/8/16 16:38
 */

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use App\Http\Models\User;
use App\Http\Models\UserKillStatus;
use Illuminate\Support\MessageBag;

class KillUserController extends Controller
{
    use ModelForm;
    protected $headerStr = '秒杀';
    protected $type = 0; //1创建 ,2修改

    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->headerStr . '管理');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('秒杀管理');
            $content->description('秒杀用户权限管理');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        $this->type = 2;
        return Admin::content(function (Content $content) use ($id) {
            $content->header('秒杀管理');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        $this->type = 1;
        return Admin::content(function (Content $content) {
            $content->header('秒杀管理');
            $content->row(function ($row) {
                $row->column(6, $this->sreach_grid());
                $row->column(6, $this->form());
            });
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(UserKillStatus::class, function (Grid $grid) {
            $grid->model()
                ->select('users_kill_status.id', 'users.name', 'users.avatar', 'users.phone', 'users_kill_status.status','users_kill_status.user_id')
                ->leftjoin('users', 'users.id', '=', 'users_kill_status.user_id');
            $grid->id('编号')->sortable();
            $grid->name('用户ID--昵称')->display(function ($name){
                return $this->user_id.'---('.$name.')';
            });
            $grid->avatar('头像')->display(function ($avatar) {
                return user_image_url($avatar);
            })->image(40, 40);
            $grid->phone('手机号');
            $grid->status('秒杀权限')->display(function ($released) {
                return $released ? '<span class=" label label-success">已授权</span>' : '<span class=" label label-default">已取消授权</span>';
            });
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();//禁用查询过滤器
                $filter->like('name', '用户昵称:');
                $filter->equal('phone', '手机号');
            });
            $grid->disableRowSelector();
            //$grid->disableCreateButton();
            $grid->disableExport();
            $grid->actions(function ($actions) {
                $actions->disableDelete();
                //$actions->disableEdit();
                if (method_exists($actions, 'disableView')) {
                    $actions->disableView();
                }
            });
            $grid->perPages([10, 20, 30, 40, 50]);
            $grid->paginate(env('Admin_Limit'));
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(UserKillStatus::class, function (Form $form) {
            $form->display('id', 'ID:');
            $form->disableReset();
            $form->text('user_id', '用户ID')->placeholder('请在左侧进行搜索,然后将用户id输入到此此栏!!,务必输入正确!');
            $options = $this->type == 2 ? [0 => '关闭秒杀权限',1 => '开放秒杀权限'] : [1 => '开放秒杀权限'];
            $form->select('status', '用户权限')->options($options);
            $form->tools(function (Form\Tools $tools) {
                $tools->disableBackButton();// 去掉返回按钮
                // $tools->disableListButton(); // 去掉跳转列表按钮
                //$tools->disableDelete(); // 去掉跳转列表按钮
                //$tools->disableView(); // 去掉跳转列表按钮
            });
            $form->saving(function (Form $form) {
                if (empty($form->model()->id)) {
                    $userinfo = User::where(['id' => request('user_id')])->first();
                    if (empty($userinfo)) {
                        $error = new MessageBag([
                            'title' => '出错!!',
                            'message' => '不存在此用户!,请在左侧筛选好后,重新填写!',
                        ]);
                        return back()->with(compact('error'));
                    }
                    $user_status = UserKillStatus::where(['user_id' => request('user_id')])->first();
                    if ($user_status) {
                        $error = new MessageBag([
                            'title' => '出错!!',
                            'message' => '该用户已经存在秒杀权限记录,请直接进行修改!',]);
                        return back()->with(compact('error'));
                    }
                }
            });
        });
    }

    protected function sreach_grid()
    {
        return Admin::grid(User::class, function (Grid $grid) {
            $grid->id('用户ID')->sortable();
            $grid->name('昵称');
            $grid->avatar('头像')->image( 40, 40);
            $grid->phone('手机号');
            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->perPages([10, 20, 30, 40, 50]);
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();//禁用查询过滤器
                $filter->like('name', '用户昵称');
                $filter->like('phone', '手机号');
            });
            $grid->expandFilter();
            $grid->actions(function ($actions) {
                $actions->disableDelete();
                //$actions->disableView();
                $actions->disableEdit();
            });
            $grid->disableActions();
            $grid->paginate(env('Admin_Limit'));
        });
    }
}